//
//  ThemeFactory.m
//  AFNetworking
//
//  Created by baboy on 2019/2/18.
//

#import <Foundation/Foundation.h>
#import "ThemeFactory.h"
#import "DBCache.h"
#import "UIImage+x.h"
#import "NSString+x.h"
#import "BKButton.h"
#import "UILabel+x.h"
#import "NSAttributedString+x.h"


@implementation ThemeFactory



+ (UIBarButtonItem *) navBarButtonForKey:(NSString *)key withTarget:(id)target action:(SEL)action{
    
    NSString *v = [DBCache valueForKey:key domain:@"Theme"];
    if (!v) {
        v = key;
    }
    return createBarImageButtonItem(v, target, action);
}
+ (UIBarButtonItem *) navBarButtonForKey:(NSString *)key{
    return [self navBarButtonForKey:key withTarget:nil action:nil];
}
+ (UIButton *) buttonForKey:(NSString *)key withTarget:(id)target action:(SEL)action{
    NSString *v = [DBCache valueForKey:key domain:@"Theme"];
    if (!v) {
        v = key;
    }
    UIButton *btn = createImageButton(CGRectZero, v, target, action);
    return btn;
}
+ (UIButton *) buttonForKey:(NSString *)key{
    return [self buttonForKey:key withTarget:nil action:nil];
}

+ (UIButton *) buttonWithTitle:(NSString *)title background:(NSString *)imageName  target:(id)target action:(SEL)action{
    UIButton *btn = createButton(CGRectZero, title, imageName, target, action);
    [btn setTitleColor:[Theme colorForKey:@"button-title-color"] forState:UIControlStateNormal];
    [btn setTitleColor:[Theme colorForKey:@"button-title-color-selected"] forState:UIControlStateSelected];
    return btn;
}

+ (UILabel *) labelForStyle:(NSString *)style{
    return createLabel(CGRectZero,
                       [Theme fontForKey:[NSString stringWithFormat:@"%@-title-font",style]],
                       nil,
                       [Theme colorForKey:[NSString stringWithFormat:@"%@-title-color",style]],
                       [Theme colorForKey:[NSString stringWithFormat:@"%@-title-shadow-font",style]],
                       CGSizeMake(0, 1),
                       NSTextAlignmentCenter,
                       1, NSLineBreakByTruncatingTail);
}
+ (UIButton *) buttonForStyle:(NSString *)style
                    withTitle:(NSString *)title
                        frame:(CGRect)frame
                       target:(id)target
                       action:(SEL)action{
    UIFont *titleFont = [Theme fontForKey:[NSString stringWithFormat:@"button-%@-title-font", style]];
    UIColor *titleColor = [Theme colorForKey:[NSString stringWithFormat:@"button-%@-title-color", style]];
    id background = [Theme colorForKey:[NSString stringWithFormat:@"button-%@-background-color", style]];
    if (!background) {
        background = [DBCache valueForKey:[NSString stringWithFormat:@"button-%@-background-image", style] domain:@"Theme"];
    }
    UIButton *btn = createButton(frame, title, background, target, action);
    if (titleFont){
        btn.titleLabel.font = titleFont;
    }
    if (titleColor){
        [btn setTitleColor:titleColor forState:UIControlStateNormal];
    }
    return btn;
}
+ (void) setStyle:(NSString *)style forButton:(UIButton *)button{
    if (style) {
        style = [NSString stringWithFormat:@"-%@", style];
    }else{
        style = @"";
    }
    UIFont *titleFont = [Theme fontForKey:[NSString stringWithFormat:@"button%@-title-font", style]];
    if (titleFont){
        button.titleLabel.font = titleFont;
    }
    
    UIColor *titleColor = [Theme colorForKey:[NSString stringWithFormat:@"button%@-title-color", style]];
    if (titleColor){
        [button setTitleColor:titleColor forState:UIControlStateNormal];
    }
    UIColor *titleShadowColor = [Theme colorForKey:[NSString stringWithFormat:@"button%@-title-shadow-color", style]];
    if (titleShadowColor){
        button.titleLabel.shadowOffset = CGSizeMake(1, 1);
        [button setTitleShadowColor:titleShadowColor forState:UIControlStateNormal];
    }
    id backgroundColor = [Theme colorForKey:[NSString stringWithFormat:@"button%@-background-color", style]];
    if (backgroundColor) {
        button.backgroundColor = backgroundColor;
    }
}
+ (void) setButtonDefaultStyle:(UIButton *)button{
    [self setStyle:nil forButton:button];
}
+ (void) setButtonPrimaryStyle:(UIButton *)button{
    [self setStyle:@"primary" forButton:button];
}
+ (UIBarButtonItem *) navButtonForStyle:(NSString *)style withTitle:(NSString *)title frame:(CGRect)frame target:(id)target action:(SEL)action{
    UIButton *btn = [self buttonForStyle:style withTitle:title frame:frame target:target action:action];
    UIFont *font = [Theme fontForKey:@"navbar-item-title-font"];
    btn.titleLabel.font = font;
    UIBarButtonItem *barBtn = /*AUTORELEASE*/([[UIBarButtonItem alloc] initWithCustomView:btn]);
    return barBtn;
}
@end


void setButtonImage(UIButton *button,NSString *imageName, NSString *imageName2, BOOL isBackground){
    UIImage *image = nil, *image2 = nil, *selectImage = nil;
    if ([imageName isKindOfClass:[UIColor class]]) {
        image = [UIImage imageWithColor:(id)imageName size:CGSizeMake(5, 5)];
        image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(2, 2, 2, 2)];
    }else{
        image = [UIImage imageNamed:imageName];
        if (!image && ![imageName hasSuffix:@"-0"]) {
            image = [UIImage imageNamed:[NSString stringWithFormat:@"%@-0", imageName]];
            if (image) {
                imageName = [NSString stringWithFormat:@"%@-0", imageName];
            }
        }
        if (!imageName2) {
            imageName2 = [imageName stringByReplacingOccurrencesOfString:@"-0" withString:@"-1"];
        }
        image2 = [imageName2 isEqualToString:imageName]?nil:[UIImage imageNamed:imageName2];
        image = image?:[UIImage imageWithContentsOfFile:imageName];
        image2 = image2?:[UIImage imageWithContentsOfFile:imageName2];
        NSString *imageSelectName = [imageName stringByReplacingOccurrencesOfString:@"-0" withString:@"-selected"];
        selectImage = [UIImage imageNamed:imageSelectName];
    }
    
    if (isBackground) {
        image = image?[image resizableImageWithCapInsets:UIEdgeInsetsMake(image.size.height/2, image.size.width/2, image.size.height/2, image.size.width/2)]:nil;
        image2 = image2?[image2 resizableImageWithCapInsets:UIEdgeInsetsMake(image2.size.height/2, image2.size.width/2, image2.size.height/2, image2.size.width/2)]:image;
        selectImage = selectImage?[selectImage resizableImageWithCapInsets:UIEdgeInsetsMake(selectImage.size.height/2, selectImage.size.width/2, selectImage.size.height/2, selectImage.size.width/2)]:image;
        [button setBackgroundImage:image forState:UIControlStateNormal];
        [button setBackgroundImage:image2 forState:UIControlStateHighlighted];
        [button setBackgroundImage:selectImage forState:UIControlStateSelected];
    }else{
        [button setImage:image forState:UIControlStateNormal];
        [button setImage:image2 forState:UIControlStateHighlighted];
        [button setImage:selectImage forState:UIControlStateSelected];
    }
}
UIButton *createButton(CGRect frame, NSString *title, id imgName,id target, SEL action){
    if ( (frame.size.width == 0 || frame.size.height == 0) && [title length] > 0 ) {
        CGSize size = title ? [NSAttributedString sizeOfString:title withFont:THEME_BTN_TITLE_FONT] : CGSizeMake(48, 28);
        frame = CGRectMake(0,0, MAX(size.width+20, 44), size.height+10);
    }
    if ( ( !title || [title isEqualToString:@""] ) && ![imgName isKindOfClass:[UIColor class]] ) {
        UIImage *backgroundImage = [UIImage imageNamed:imgName];
        if (backgroundImage) {
            frame.size = CGSizeMake( backgroundImage.size.width, backgroundImage.size.height);
        }
    }
    UIButton *btn = ([[BKButton alloc] initWithFrame:frame]);
//    btn.titleLabel.font = gButtonTitleFont;
//    btn.titleLabel.textColor = gButtonTitleColor;
    [btn setTitle:title forState:UIControlStateNormal];
    setButtonImage(btn, imgName, nil,title?YES:NO);
    if (target)
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

UIButton *createImageButton(CGRect frame, NSString *imageName, id target, SEL action){
    if (frame.size.width == 0 || frame.size.height == 0) {
        frame.size = sizeOfImage(imageName);
    }
    UIButton *btn = createButton(frame, nil,imageName, target, action);
    return btn;
}

UIBarButtonItem * createBarImageButtonItem(NSString *iconName,id target,SEL action){
    UIButton *btn = createImageButton(CGRectZero, iconName, target, action);
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
    return item ;
}

UIBarButtonItem * createBarButtonItem(NSString *title,id target,SEL action){
    //UIImage *btnBg = [UIImage imageNamed:@"btn_bg.png"];
    //[btnBg stretchableImageWithLeftCapWidth:btnBg.size.height/2 topCapHeight:btnBg.size.width/2];
    CGSize tsize = [NSAttributedString sizeOfString:title withFont:THEME_BTN_TITLE_FONT];
    CGRect rect = CGRectMake(0, 0, tsize.width+12, 28);
    
    UIButton *btn = [[BKButton alloc] initWithFrame:rect] ;
    //[btn setBackgroundImage:btnBg forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn.titleLabel setFont:THEME_BTN_TITLE_FONT];
    [btn setTitleColor:THEME_BTN_TITLE_COLOR  forState:UIControlStateNormal];
    [btn.layer setBorderColor:[UIColor colorWithWhite:0.96 alpha:1.0].CGColor];
    [btn.layer setBorderWidth:1.0];
    //[btn setTitleShadowColor:gButtonTitleShadowColor forState:UIControlStateNormal];
    // [btn.titleLabel setShadowOffset:CGSizeMake(0, -1)];
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
    [item setTarget:target];
    [item setAction:action];
    return item ;
}
